package edit;

import pulpcore.Input;
import pulpcore.image.Colors;
import pulpcore.sprite.Sprite;
import pulpcore.sprite.FilledSprite;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import draw.DrawBoxSprite;
import draw.DrawCircleSprite;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.pulp.body.BodySprite;
import pulpfizz.pulp.body.Java2DBodySprite;

import java.awt.*;

import goliath.BasicDemo;

/**
 * @author will
 * @date Aug 9, 2008 8:34:42 PM
 */
public class DrawBoxContext extends EditContext {
    Vec2 startPos;
    Sprite drawSprite;

    boolean pressed;
    
    public DrawBoxContext() {
        super();
        startPos = new Vec2();
    }

    public void update(int elapsedTime) {
        if(Input.isMousePressed()) {
            // Record the starting position
            startPos.set(mousePos);

            drawSprite = new DrawBoxSprite(this);
            layer.add(drawSprite);
            pressed = true;
        }

        if(Input.isMouseReleased() && pressed) {
            pressed = false;
            Vec2 start = startPos.add(mousePos).mul(0.5f);
            Vec2 dim = startPos.sub(mousePos).abs();
            Body myBox = BodyUtils.createBox(layer.getWorld(),start.x,start.y,dim.x,dim.y);
            if(!BasicDemo.isFixed()) {
                Material.DEAD_WEIGHT().applyAll(myBox);
            }
            Java2DBodySprite bs = new Java2DBodySprite(myBox,layer,5, BasicDemo.getDrawColor(), BasicDemo.getFillColor());
            layer.add(bs);

            layer.remove(drawSprite);
        }


    }

    public void reset() {
        startPos.setZero();
        drawSprite = null;
        pressed = false;
    }

    public String toString() {
        return "Box";
    }


}
